﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZCalculator.Core
{
    public abstract class Operator : ExpressionNode
    {
        public abstract int Priority { get; }
    }

    public class BinaryOperator : Operator
    {
        public static bool CharIsBinaryOperator(char c)
        {
            return c == '+' || c == '-' || c == '*' || c == '/';
        }
        
        private char _operator;
        public ExpressionNode LNode, RNode;
        public BinaryOperator(char op) { _operator = op; }
        public BinaryOperator(char op, ExpressionNode l, ExpressionNode r)
            : this(op)
        {
            LNode = l;
            RNode = r;
        }
        public override int Priority
        {
            get
            {
                if (_operator == '+' || _operator == '-')
                    return 5;
                else
                    return 10;
            }
        }
        public override double Evaluate()
        {
            switch (_operator)
            {
                case '+':
                    return LNode.Evaluate() + RNode.Evaluate();
                case '-':
                    return LNode.Evaluate() - RNode.Evaluate();
                case '*':
                    return LNode.Evaluate() * RNode.Evaluate();
                case '/':
                    return LNode.Evaluate() / RNode.Evaluate();
                default:
                    throw new NotSupportedException();
            }
        }
    }
}
